package com.yyfax.ph.personal.authority.bankcard.model;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by ying_he on 2017/5/23.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class UnbindCardModel extends Model {
	private static final String TAG = UnbindCardModel.class.getSimpleName();
	public static final int MSG_CHECK_UNBIND = 10;
	public static final int MSG_UNBIND_CARD = 11;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode) {
			case MSG_CHECK_UNBIND:
				return service.checkUnbind(token, new EmptyEntity());
			case MSG_UNBIND_CARD:
				return service.unbindCard(token, new EmptyEntity());
		}
		return null;
	}

	@Override
	public void onNext(Object entity) {
		CLog.i(TAG, "onNext: " + entity.toString());
		BaseResultEntity result = cast(entity);
		switch (result.requestCode) {
			case MSG_CHECK_UNBIND:
				CheckUnbindResult checkunbindResult = cast(entity);
				EventAgent.post(checkunbindResult);
				break;
			case MSG_UNBIND_CARD:
				UnbindCardResult unbindResult = cast(entity);
				EventAgent.post(unbindResult);
				break;
		}

	}

	public interface Service {
		@POST("account/card/unbind")
		Observable<UnbindCardResult> unbindCard(@Header("token") String token, @Body EmptyEntity params);

		@POST("account/card/checkunbind")
		Observable<CheckUnbindResult> checkUnbind(@Header("token") String token, @Body EmptyEntity params);
	}

	public static class CheckUnbindResult extends BaseResultEntity<CheckUnbindBean> {

	}

	public static class CheckUnbindBean {
		public String code;
	}

	public static class UnbindCardResult extends BaseResultEntity<UnbindCardBean> {

	}

	public static class UnbindCardBean {
		public String name;
		public String certId;
	}
}
